package game.platformer.interfaces;
import android.graphics.Color;
import android.graphics.Typeface;
import game.platformer.Player;
import game.platformer.platformer;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.Texture;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.opengl.texture.region.TextureRegionFactory;
import org.anddev.andengine.util.HorizontalAlign;
/**
 *
 * @author alex
 */
public class MainMenuScreen extends Scene {
	//Textures---------------
	private Texture mainScreenBTN, logo, fontT;
	private TextureRegion tr_mainScreenBTN, tr_logo;
	private platformer game;
	private Font myFont;
	//-----------------------
	int camWidth = 720;
	int camHeight = 480;
	public MainMenuScreen(platformer game, Camera cam){
		this.game = game;
		loadResources(game, cam);
		display();
	}
	private void loadResources(platformer game, Camera cam){
		mainScreenBTN = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		tr_mainScreenBTN = TextureRegionFactory.createFromAsset(mainScreenBTN, game, "gfx/mainScreenBTN.png", 0, 0);

		logo = new Texture(256, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		tr_logo = TextureRegionFactory.createFromAsset(logo, game, "gfx/logo.png", 0, 0);


		this.fontT = new Texture(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.myFont = new Font(this.fontT, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32, true, Color.WHITE);

		//Load Textures & Fonts
		game.getEngine().getTextureManager().loadTextures(mainScreenBTN, logo, fontT);
		game.getEngine().getFontManager().loadFont(this.myFont);
	}
	private void display(){
		setBackground(new ColorBackground(0, 0, 0));

		//Create TXT files to overlay buttons.
		final Text newGameTXT = new Text(15, camHeight - 3 * tr_mainScreenBTN.getHeight() + 10, this.myFont, "New Game", HorizontalAlign.CENTER);
		final Text loadGameTXT = new Text(15, camHeight - 2 * tr_mainScreenBTN.getHeight() + 10, this.myFont, "Load Game", HorizontalAlign.CENTER);
		final Text optionsTXT = new Text(15, camHeight - tr_mainScreenBTN.getHeight() + 10, this.myFont, "Options", HorizontalAlign.CENTER);


		final Sprite s_logo = new Sprite((camWidth - tr_logo.getWidth()) / 2, 0, tr_logo);


		final Sprite newGameBTN = new Sprite(0, camHeight - 3 * tr_mainScreenBTN.getHeight(), tr_mainScreenBTN) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){
				Player.getPlayer().resetPlayer();
				platformer.getLevelManager().loadLevel(0, 110, 100);
				platformer.getScreenManager().activateMapScreen();
				return true;
			}
		};

		final Sprite loadGameBTN = new Sprite(0, camHeight - 2 * tr_mainScreenBTN.getHeight(), tr_mainScreenBTN) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){
				platformer.getScreenManager().activateMapScreen();
				return true;
			}
		};

		final Sprite optionsBTN = new Sprite(0, camHeight - tr_mainScreenBTN.getHeight(), tr_mainScreenBTN) {
			@Override
			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY){
				platformer.getLevelManager().loadLevel(3, 100, 50);
				platformer.getScreenManager().activateMapScreen();
				return true;
			}
		};

		//Attach images to scene
		attachChild(s_logo);
		attachChild(newGameBTN);
		attachChild(loadGameBTN);
		attachChild(optionsBTN);

		//Attach text to scene
		attachChild(newGameTXT);
		attachChild(loadGameTXT);
		attachChild(optionsTXT);

		//attatch touch areas
		registerTouchArea(newGameBTN);
		registerTouchArea(loadGameBTN);
		registerTouchArea(optionsBTN);
		//turn on touch bs
		setTouchAreaBindingEnabled(true);
	}
}
